package com.awakening.servlet;

import com.awakening.pojo.User;
import com.awakening.service.UserService;
import com.awakening.service.impl.UserServiceImpl;
import com.sun.org.apache.bcel.internal.classfile.Code;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户激活Serlvet
 * @Author:AwakeningCode
 * @Date: Created in 23:04 2018\1\22 0022
 */
public class ActiveServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //处理中文乱码
            req.setCharacterEncoding("UTF-8");
            //接收用户激活码
            String code = req.getParameter("code");
            UserService userService = new UserServiceImpl();
            User user = userService.findByCode(code);
            //根据激活码查询用户
            if (user != null){
                //已经查询到用户，修改用户信息
                user.setState(1);
                user.setCode("");
                userService.update(user);
                req.setAttribute("msg","您的邮箱激活成功，欢迎加入YanBaby社区！");
            }else {
                //根据激活码没有查询到用户
                req.setAttribute("msg","您的激活码有误，请重新激活！");
            }
            req.getRequestDispatcher("/msg.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
